rm(list=ls())

setwd('put_your_wd_here')

pacman::p_load(tidyverse, rethnicity, patchwork)

# this code uses the predict_ethnicity function from rethnicity package
# to estimate race of victims based on list of victim names
# we then went and hand verified race based on media coverage of shootings
# see paper for methodology

# race <- read_csv('/Users/tyler/Dropbox/Mass Shootings and Tweets, Trends, and Donations/data/raceofvictims/race shooting 5.csv')
# 
# results <- predict_ethnicity(firstnames=race$victimfirst,
#                   lastnames=race$surname,
#                   method='fullname')
# results$shooting <- race$shooting
# write_csv(results, '/Users/tyler/Dropbox/Mass Shootings and Tweets, Trends, and Donations/data/raceofvictims/race_shooting_predict_ethnicity.csv')

media <- read_csv('datasets/media_coverage_by_shooting.csv')
race <- read_csv('datasets/race_shooting_predict_ethnicity.csv')

race <- race %>%
  group_by(shooting) %>%
  summarise(mean.white = mean(race == 'white'),
            n=n()) 

dat <- media %>%
  dplyr::select(shooting, total_media) %>%
  right_join(race, by='shooting') 
p1 <- dat %>%
  ggplot(aes(x=mean.white, y=total_media)) +
  stat_smooth(method='lm', linetype=2, size=0.5, color='red') +
  geom_point() + 
  ggrepel::geom_text_repel(aes(label=shooting),max.overlaps = 3) + 
  theme_bw() +
  labs(x='Pct Victims White', y='Media Coverage')

p2 <- dat %>%
  ggplot(aes(x=n, y=total_media)) +
  stat_smooth(method='lm', linetype=2, size=0.5, color='red') +
  geom_point() + 
  ggrepel::geom_text_repel(aes(label=shooting),max.overlaps = 3) + 
  theme_bw() +
  labs(x='Number of Victims', y='Media Coverage')

p <- p1 + p2
p
ggsave(p, width=8, height=3.5, 
       file='figures/race_media.pdf')

# write out data for other analyses
write_csv(dat %>% dplyr::select(shooting, mean.white), 
          file = 'datasets/shooting_race.csv')




